package com.yuyou.train.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuyou.train.admin.mapper.AuthRoleMapper;
import com.yuyou.train.admin.service.IAuthRoleService;
import com.yuyou.train.admin.vo.req.ReqAuthRoleAdd;
import com.yuyou.train.admin.vo.req.ReqAuthRoleUpdate;
import com.yuyou.train.service.api.admin.entity.AuthRole;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.message.admin.AuthRoleMess;
import com.yuyou.train.util.tool.DateUtil;
import com.yuyou.train.util.tool.DefindBeanUtils;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

/**
 * Created by yujin.liu on 2018/1/22.
 */
@Service
public class AuthRoleServiceImpl implements IAuthRoleService {

    @Autowired
    private AuthRoleMapper authRoleMapper;


    @Override
    public int findAuthRolePageCount(Pagination<AuthRole> p) {
        return authRoleMapper.findAuthRolePageCount(p);
    }


    @Override
    public List<AuthRole> findAuthRoleList() {
        return authRoleMapper.findAuthRoleList();
    }


    @Override
    public List<AuthRole> findAuthRolePageList(Pagination<AuthRole> p) {
        return authRoleMapper.findAuthRolePageList(p);
    }

    @Override
    public ResBoolSimpleInfo insertAuthRole(ReqAuthRoleAdd authRoleAdd){

        ResBoolSimpleInfo res = new ResBoolSimpleInfo();

        AuthRole authRoleQuery = authRoleMapper.selectByRoleName(authRoleAdd.getRoleName().trim());
        if(null != authRoleQuery){
            res.setSuccess(false);
            res.setMessage(AuthRoleMess.AUTHROLE_EXIST);
            return res;
        }

        AuthRole authRole = new AuthRole();
        authRole.setId(Sequence.getSequence().getNextId());
        authRole.setRoleName(authRoleAdd.getRoleName().trim());
        authRole.setCreateTime(DateUtil.now());
        authRole.setUpdateTime(DateUtil.now());
        authRoleMapper.insert(authRole);

        return ResBoolSimpleInfo.getSuccessInfo();
    }

    @Override
    @Transactional(timeout=60)
    public ResBoolSimpleInfo updateAuthRole(ReqAuthRoleUpdate reqAuthRoleUpdate) {

        ResBoolSimpleInfo res = new ResBoolSimpleInfo();
        AuthRole authRole = new AuthRole();
        DefindBeanUtils.copyProperties(reqAuthRoleUpdate, authRole);
        AuthRole authRoleQuery = authRoleMapper.selectByRoleName(authRole.getRoleName().trim());
        if(null != authRoleQuery && !authRoleQuery.getId().equals(authRole.getId())){
            res.setSuccess(false);
            res.setMessage(AuthRoleMess.AUTHROLE_EXIST);
            return res;
        }

        authRole.setUpdateTime(DateUtil.now());
        authRoleMapper.updateByPrimaryKeySelective(authRole);
        return ResBoolSimpleInfo.getSuccessInfo();
    }

    @Override
    public int deleteAuthRoleById(String id) {
        return authRoleMapper.deleteByPrimaryKey(id);
    }

}
